﻿using AutoMapper;
using Modules.Corp;
using Modules.Device;
using Modules.Device.Entitys;
using PmSoft;

namespace ManagementApi.Models.Device
{
    public class CardReaderDeviceDto
    {
        /// <summary>
        ///  主键
        ///</summary>
        public int Id { get; set; }
        /// <summary>
        /// 设备编码(序列号) 
        ///</summary>
        public string DeviceCode { get; set; }
        /// <summary>
        /// 企业编码 
        ///</summary>
        public string CorpCode { get; set; }
        /// <summary>
        /// 审核人Id 
        ///</summary>
        public int? AuditUserId { get; set; }
        /// <summary>
        /// 审核人姓名 
        ///</summary>
        public string AuditUserName { get; set; }
        /// <summary>
        /// 审核时间 
        ///</summary>
        public DateTime? AuditTime { get; set; }
        /// <summary>
        /// 状态（0：待审核，1：已审核，2：已禁用） 
        ///</summary>
        public 设备审核状态? Status { get; set; }
        /// <summary>
        /// 禁用时间 
        ///</summary>
        public DateTime? DisableTime { get; set; }
        /// <summary>
        /// 禁用原因 
        ///</summary>
        public string DisableReason { get; set; }
        /// <summary>
        /// 创建时间 
        ///</summary>
        public DateTime? CreateTime { get; set; }
        /// <summary>
        /// 设备描述 
        ///</summary>
        public string Description { get; set; }
        /// <summary>
        /// 设备信息 
        ///</summary>
        public string DeviceInfo { get; set; }
        /// <summary>
        /// 状态名称
        /// </summary>
        public string StatusName { get; set; }

        /// <summary>
        /// 设备类型名称(身份证读卡器 ,  门禁闸机等) 
        ///</summary>
        public string? DeviceTypeName { get; set; }

        public string CorpName { get; set; }
    }
    public static partial class CardReaderDeviceExceptions
    {
        public static CardReaderDeviceDto AsDto(this CardReader entity)
        {
            CardReaderDeviceDto dto = new();
            var mapper = ServiceAccessor.GetService<IMapper>();
            mapper.Map(entity, dto);
            dto.StatusName = entity.Status?.GetDescription() ?? string.Empty;

            var corpService = ServiceAccessor.GetService<CorpService>();
            CorpInfo? corp = corpService.GetCorpInfo(entity.CorpCode);
            if (corp != null) dto.CorpName = corp.Name;
            return dto;
        }
    }

}
